/**
 * Copyright (c) 2012-2013 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */
package com.nokia.example.fuelcalculator.utils;

import com.nokia.example.fuelcalculator.model.FuelConsumptionModel;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * This class is responsible for persisting FuelConsumptionModel.
 */
public class Persistor {
    
    private FuelConsumptionModel model;
    private String recordStoreName;
    
    public Persistor(FuelConsumptionModel model, String recordStoreName) {
        this.model = model;
        this.recordStoreName = recordStoreName;
    }
    
    
    /**
     * Load the state from RecordStore to the the model
     */
    public void loadState() {
        try {
            RecordStore snapshot = RecordStore.openRecordStore(recordStoreName, true);
            if (snapshot.getNextRecordID() > 0) {
                model.populate(snapshot.getRecord(getRecordId(snapshot)));
            }
            snapshot.closeRecordStore();
        } catch (RecordStoreException e) {
            System.out.println("exception occured while loading");
        } catch (NumberFormatException nfe) {
           //
        }
    }

    /**
     * Save the state of the model to RecordStore
     */
    public void saveState() {
        try {
            if (model.isEmpty()) {
                deleteRecords();
            } else {
                RecordStore snapshot = RecordStore.openRecordStore(recordStoreName, true);
                if (snapshot.getNumRecords() == 0) {
                    snapshot.addRecord(null, 0, 0);
                }
                byte[] data = model.getData();
                snapshot.setRecord(getRecordId(snapshot), data, 0, data.length);
                snapshot.closeRecordStore();
                System.out.println("state saved succesfully");
            }
        } catch (RecordStoreException e) {
            System.out.println("exception occured while saving");
        }
    }

    /*
     * If the consumption log is empty, there is no need to persist the state
     * in RMS.
     */
    private void deleteRecords() {
        try {
            RecordStore.deleteRecordStore(recordStoreName);
        } catch (RecordStoreException ex) {
            // this is acceptable, just tried to delete a record when
            // there was not one
        }
    }
    
    /**
     * Get the first Record ID for a given record store
     */
    private int getRecordId(RecordStore store) throws RecordStoreException {
        RecordEnumeration e = store.enumerateRecords(null, null, false);
        try {
            return e.nextRecordId();
        } finally {
            e.destroy();
        }
    }
}
